package com.meida.boot.handler.app;


import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.marketing.provider.service.CouponUserService;
import com.meida.module.order.provider.service.OrderAfterserviceService;
import com.meida.module.order.provider.service.OrderInfoService;
import com.meida.module.pay.client.entity.PayBalance;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.system.provider.service.SysColleconService;
import com.meida.module.system.provider.service.SysCompanyService;
import com.meida.module.vip.provider.service.VipUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 普通用户个人中心接口扩展
 *
 * @author Administrator
 */
@Component("userMineInfoHandler")
@Log4j2
public class UserMineInfoHandler implements GetInterceptor {
    @Resource
    SysCompanyService companyService;
    @Resource
    VipUserService vipUserService;
    @Resource
    SysColleconService colleconService;
    @Resource
    private PayBalanceService payBalanceService;
    @Resource
    private CouponUserService couponUserService;
    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private OrderAfterserviceService afterserviceService;


    /**
     * 查询条件扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void prepare(CriteriaQuery cq, EntityMap params) {
    }


    /**
     * 返回结果集扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long useId = OpenHelper.getUserId();
        PayBalance balance = payBalanceService.getByUserId(useId);
        BigDecimal bindAmount = new BigDecimal("0");
        Boolean isSetPayPwd = false;
        if (FlymeUtils.isNotEmpty(balance)) {
            bindAmount = balance.getBindAmount();
            if (FlymeUtils.isNotEmpty(balance.getPaypwd())) {
                isSetPayPwd = true;
            }
        }
        Integer messageNum = 0;
        Integer couponNum = couponUserService.countByUserId(useId);
        Integer serviceCount = afterserviceService.countByUserId(useId);

        EntityMap orderCount = orderInfoService.countOrderStateByUserId(useId);

        Integer unPayCount = orderCount.getInt("tab1", 0);
        Integer unSendCount = orderCount.getInt("tab20", 0);
        Integer unReceiveCount = orderCount.getInt("tab21", 0);
        Integer finishedCount = orderCount.getInt("tab3", 0);

        map.put("unPayCount", unPayCount);
        map.put("unReceiveCount", unSendCount + unReceiveCount);
        map.put("finishedCount", finishedCount);
        map.put("serviceCount", serviceCount);
        map.put("couponNum", couponNum);
        map.put("bindAmount", bindAmount);
        map.put("messageNum", messageNum);
        map.put("isSetPayPwd", isSetPayPwd);
        map.putAll(orderCount);
    }
}
